<?php

class VesocpModule extends CWebModule
{
    public $defaultController = 'trongngay';

    private $_assetsUrl;

    public function init()
    {
        // echo Yii::getPathOfAlias('admin.assets');die; 
        // this method is called when the module is being created
        // you may place code here to customize the module or the application

        // import the module-level models and components
        $this->setImport(array(
        'vesocp.models.*',
        'vesocp.components.*',
        ));

        $this->setComponents(array(
        //'errorHandler' => array('errorAction' => 'customer/default/error'),
        'user' => array(
        'class' => 'CWebUser',             
        'loginUrl' => Yii::app()->createUrl('vesocp/admin/login'),
        )
        ));

        //Yii::app()->user->setStateKeyPrefix('_admin');
    }

    public function beforeControllerAction($controller, $action)
    {   
        $url = new Url();
        if(parent::beforeControllerAction($controller, $action))
        {
            //var_dump(Yii::app()->getModule('admin')->user);die;
            $route = $controller->id . '/' . $action->id;

            $publicPages = array(
            'admin/login',
            'admin/noPermit',
            'admin/logout'
            );
            $controller->layout = 'column_admin';
            if(Yii::app()->getModule('vesocp')->user->isGuest && !in_array($route, $publicPages)){
                return true;
                Yii::app()->getModule('vesocp')->user->loginRequired();   
            }elseif(!Yii::app()->getModule('vesocp')->user->isGuest && !in_array($route, $publicPages)){
                //return true; 
                if($controller->id == "home"){
                    return true;
                }                
                $data_permit = Admin::getPermit(Yii::app()->getModule('vesocp')->user->id);
                for($i=0;$i<count($data_permit);$i++){
                     $arr_permit = explode("-",$data_permit[$i]["itemname"]);
                     if($controller->id==$arr_permit[0] && $arr_permit[1]=="all"){                        
                         return true;
                     }
                }
                
            }elseif(in_array($route, $publicPages)){
                return true;
            }
            Controller::redirect($url->createUrl("admin/noPermit")); 
        }
        else
            return false; 
    }

    /**
    * @return string the base URL that contains all published asset files of this module.
    */
    public function getAssetsUrl()
    {
        if($this->_assetsUrl===null)
            $this->_assetsUrl=Yii::app()->getAssetManager()->publish(Yii::getPathOfAlias('vesocp.assets'));

        return $this->_assetsUrl;
    }

    /**
    * @param string the base URL that contains all published asset files of this module.
    */
    public function setAssetsUrl($value)
    {
        $this->_assetsUrl=$value;
    }

    public function registerCss($file, $media='all')
    {
        $href = $this->getAssetsUrl().'/css/'.$file;
        return '<link rel="stylesheet" type="text/css" href="'.$href.'" media="'.$media.'" />';
    }

    public function registerJs($file)
    {
        $href = $this->getAssetsUrl().'/js/'.$file;
        return '<script type="text/javascript" src="'.$href.'"></script>';
    }

    public function registerImage($file)
    {
        return $this->getAssetsUrl().'/images/'.$file;
    }
}
